﻿using UnityEngine;
using ZCUtil.Touch;

namespace ZCTouchDrawBoard
{
    public class MagicPaint : BasePaint
    {
        #region ConstField
        private const string SHADER_NAME = "Hidden/MagicPenShader";
        private const int BACKGROUND_TEXTURE_INDEX = 1;
        #endregion

        #region PrivateField
        private ColorPaint m_ColorPaint;
        private Vector2 m_BackgroundSize;
        private Texture2D m_BackgroundTex;
        #endregion

        #region PublicMethod
        public override PaintAttribute PaintAttribute
        {
            get
            {
                return m_PaintModel;
            }
            set
            {
                if (value == null)
                {
                    return;
                }
                else
                {
                    if (value.MaterialPaths != null && value.MaterialPaths.Length >= 2)
                    {
                        if (m_PaintModel.MaterialPaths == null)
                        {
                            m_Channels[BACKGROUND_TEXTURE_INDEX] = Resources.Load<Texture2D>(value.MaterialPaths[1]);
                            m_BackgroundSize = new Vector2(m_Channels[BACKGROUND_TEXTURE_INDEX].width, m_Channels[BACKGROUND_TEXTURE_INDEX].height);
                        }
                        else if (!value.MaterialPaths[1].Equals(m_PaintModel.MaterialPaths[1]))
                        {
                            m_Channels[BACKGROUND_TEXTURE_INDEX] = Resources.Load<Texture2D>(value.MaterialPaths[1]);
                            m_BackgroundSize = new Vector2(m_Channels[BACKGROUND_TEXTURE_INDEX].width, m_Channels[BACKGROUND_TEXTURE_INDEX].height);
                        }
                        m_ColorPaint.PaintAttribute = value;
                        m_ColorPaint.PaintAttribute.PenColor = Color.white;
                        m_PaintModel.Copy(value);
                    }
                }
            }
        }
        #endregion

        #region PublicMethod
        public MagicPaint() : base(SHADER_NAME)
        {
            m_ColorPaint = new ColorPaint();
        }

        public MagicPaint(PaintAttribute paintModel) : base(paintModel, SHADER_NAME)
        {
            m_ColorPaint = new ColorPaint(paintModel);
            m_ColorPaint.PaintAttribute.PenColor = Color.white;
        }

        public override void DrawToPaper(TouchPoint touch, BasePaperView paper, ePaintEvent paintEvent, Mesh mesh = null)
        {
            if (paintEvent == ePaintEvent.Down)
            {
                m_DoublePaper.ClearB();
            }
            DrawToPaper(touch.Points, paper, mesh);
        }

        public override void DrawToPaper(Vector3[] positions, BasePaperView paper, Mesh mesh = null)
        {
            m_ColorPaint.DrawToPaper(positions, m_DoublePaper.PaperB, paper.OrthoMatrix, mesh);
            // 绘制和显示
            paper.Use();
            // 设置ShaderPass
            m_Shader.SetTexture("_BackgroundTex", m_Channels[BACKGROUND_TEXTURE_INDEX]);
            m_Shader.SetTexture("_LineMaskTex", m_DoublePaper.PaperB.RenderTexture);
            m_Shader.SetColor("_Color", PaintAttribute.PenColor);
            m_Shader.SetVector("_BackgroundScale", new Vector2(paper.Width, paper.Height) / m_BackgroundSize * 20.0f);
            m_Shader.SetPass(0);
            Graphics.DrawMeshNow(mesh, Matrix4x4.identity);
            paper.Display();
        }
        #endregion
    }
}